package com.shycloud.mido.homework.service.impl;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shycloud.mido.homework.api.entity.ZebraHomeworkLeaveMessage;
import com.shycloud.mido.homework.api.vo.ZebraHomeworkLeaveMessageVO;
import com.shycloud.mido.homework.mapper.ZebraHomeworkLeaveMessageMapper;
import com.shycloud.mido.homework.service.ZebraHomeworkLeaveMessageService;
import lombok.AllArgsConstructor;

@Service
@AllArgsConstructor
public class ZebraHomeworkLeaveMessageServiceImpl extends ServiceImpl<ZebraHomeworkLeaveMessageMapper, ZebraHomeworkLeaveMessage> implements ZebraHomeworkLeaveMessageService {

	/** 作业辅导留言模块 mapper */
	private ZebraHomeworkLeaveMessageMapper leaveMessageMapper;

	/**
	 * 获取作业辅导留言面板数据
	 *
	 * @param homeworkId
	 * @return java.util.List<com.shycloud.mido.homework.api.vo.ZebraHomeworkLeaveMessageVO>
	 * @author nianhua.jiang
	 * @date 2021/1/13 15:05
	 **/
	@Override
	public List<ZebraHomeworkLeaveMessageVO> getLeaveMessage(String homeworkId) {

		List<ZebraHomeworkLeaveMessage> leaveMessagesList = leaveMessageMapper.selectList(new QueryWrapper<ZebraHomeworkLeaveMessage>().eq("homework_id", homeworkId));

		List<ZebraHomeworkLeaveMessageVO> resList = leaveMessagesList.stream().map(x -> {
			ZebraHomeworkLeaveMessageVO zebraLeaveMessage = new ZebraHomeworkLeaveMessageVO();
			BeanUtils.copyProperties(x, zebraLeaveMessage);
			return zebraLeaveMessage;
		}).collect(Collectors.toList());

		for (ZebraHomeworkLeaveMessageVO zebra : resList) {
			if (zebra.getCreateTime().plusDays(30).isBefore(LocalDateTime.now())) {
				zebra.setIsExpire("1");
			} else {
				zebra.setIsExpire("0");
			}
		}

		return resList;
	}
}
